home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Taifun / Taifun 175 (1991-09-10)(Manewaldt, A.)(DE)(PD).zip / Taifun 175 (1991-09-10)(Manewaldt, A.)(DE)(PD).adf / Term / Source.LZH / Beep.c next >
C/C++ Source or Header  |  1991-07-06  |  7KB  |  304 lines

  1. /* $Revision Header * Header built automatically - do not edit! *************
  2.  *
  3.  *    (C) Copyright 1990 by Olaf 'Olsen' Barthel & MXM
  4.  *
  5.  *    Name .....: Beep.c
  6.  *    Created ..: Monday 21-Jan-91 20:12
  7.  *    Revision .: 0
  8.  *
  9.  *    Date            Author          Comment
  10.  *    =========       ========        ====================
  11.  *    21-Jan-91       Olsen           Created this file!
  12.  *
  13.  * $Revision Header ********************************************************/
  14.  
  15. #include "TermGlobal.h"
  16.  
  17.     /* IFF Sound `8SVX' voice header. */
  18.  
  19. struct Voice8Header
  20. {
  21.     ULONG    oneShotHiSamples,    /* # samples in the high octave 1-shot part */
  22.         repeatHiSamples,    /* # samples in the high octave repeat part */
  23.         samplesPerHiCycle;    /* # samples/cycle in high octave, else 0 */
  24.     UWORD    samplesPerSec;        /* data sampling rate */
  25.     UBYTE    ctOctave,        /* # of octaves of waveforms */
  26.         sCompression;        /* data compression technique used */
  27.     LONG    volume;            /* playback nominal volume from 0 to Unity
  28.                      * (full volume). Map this value into
  29.                      * the output hardware's dynamic range.
  30.                      */
  31. };
  32.  
  33.     /* Small sound information. */
  34.  
  35. struct SoundInfo
  36. {
  37.     APTR    Data;        /* Data pointer. */
  38.  
  39.     LONG    Length;        /* Real length. */
  40.     ULONG    Rate;        /* Recording rate. */
  41.     SHORT    Volume;        /* Sound volume. */
  42. };
  43.  
  44.     /* Local sound info. */
  45.  
  46. STATIC struct SoundInfo    SoundInfo;
  47. STATIC BYTE        HasSound = FALSE,SoundPlayed = FALSE;
  48.  
  49.     /* CreateBeep():
  50.      *
  51.      *    Set up the audio.device for a decent beep sound.
  52.      */
  53.  
  54. BYTE
  55. CreateBeep()
  56. {
  57.     if(!AudioBlock)
  58.     {
  59.         struct MsgPort *AudioPort;
  60.  
  61.         SoundPlayed = FALSE;
  62.  
  63.         if(AudioPort = (struct MsgPort *)CreateMsgPort())
  64.         {
  65.             if(AudioBlock = (struct IOAudio *)CreateIORequest(AudioPort,sizeof(struct IOAudio)))
  66.             {
  67.                 if(!OpenDevice(AUDIONAME,0,AudioBlock,0))
  68.                 {
  69.                     AudioBlock -> ioa_Request . io_Command                = ADCMD_ALLOCATE;
  70.                     AudioBlock -> ioa_Request . io_Message . mn_Node . ln_Pri    = 10;
  71.                     AudioBlock -> ioa_Data                        = &AnyChannel[0];
  72.                     AudioBlock -> ioa_Length                    = 4;
  73.  
  74.                     if(!DoIO(AudioBlock))
  75.                     {
  76.                         if(HasSound)
  77.                         {
  78.                             AudioBlock -> ioa_Request . io_Command    = CMD_WRITE;
  79.                             AudioBlock -> ioa_Request . io_Flags    = ADIOF_PERVOL;
  80.                             AudioBlock -> ioa_Period        = SoundInfo . Rate;
  81.                             AudioBlock -> ioa_Volume        = SoundInfo . Volume;
  82.                             AudioBlock -> ioa_Cycles        = 1;
  83.                             AudioBlock -> ioa_Data            = SoundInfo . Data;
  84.                             AudioBlock -> ioa_Length        = SoundInfo . Length;
  85.                         }
  86.                         else
  87.                         {
  88.                             AudioBlock -> ioa_Request . io_Command    = CMD_WRITE;
  89.                             AudioBlock -> ioa_Request . io_Flags    = ADIOF_PERVOL;
  90.                             AudioBlock -> ioa_Period        = 223;
  91.                             AudioBlock -> ioa_Volume        = 64 / 2;
  92.                             AudioBlock -> ioa_Cycles        = 150;
  93.                             AudioBlock -> ioa_Data            = &SineWave[0];
  94.                             AudioBlock -> ioa_Length        = 8;
  95.                         }
  96.  
  97.                         return(TRUE);
  98.                     }
  99.  
  100.                     CloseDevice(AudioBlock);
  101.                 }
  102.  
  103.                 DeleteIORequest(AudioBlock);
  104.             }
  105.  
  106.             DeleteMsgPort(AudioPort);
  107.         }
  108.  
  109.         AudioBlock = NULL;
  110.  
  111.         return(FALSE);
  112.     }
  113.     else
  114.         return(TRUE);
  115. }
  116.  
  117.     /* DeleteBeep():
  118.      *
  119.      *    Remove the data allocated for the beep sound.
  120.      */
  121.  
  122. VOID
  123. DeleteBeep()
  124. {
  125.     if(AudioBlock)
  126.     {
  127.         if(AudioBlock -> ioa_Request . io_Device)
  128.         {
  129. /*            if(!CheckIO(AudioBlock))
  130.             {
  131.                 AbortIO(AudioBlock);
  132.  
  133.                 WaitIO(AudioBlock);
  134.             }*/
  135.  
  136.             CloseDevice(AudioBlock);
  137.         }
  138.  
  139.         if(AudioBlock -> ioa_Request . io_Message . mn_ReplyPort)
  140.             DeleteMsgPort(AudioBlock -> ioa_Request . io_Message . mn_ReplyPort);
  141.  
  142.         DeleteIORequest(AudioBlock);
  143.  
  144.         AudioBlock = NULL;
  145.     }
  146.  
  147.     if(HasSound)
  148.     {
  149.         FreeVec(SoundInfo . Data);
  150.  
  151.         HasSound = FALSE;
  152.     }
  153. }
  154.  
  155.     /* Beep():
  156.      *
  157.      *    Produce a decent beep sound.
  158.      */
  159.  
  160. VOID
  161. Beep()
  162. {
  163.     if(AudioBlock)
  164.     {
  165.             /* AudioRequest has returned. */
  166.  
  167.         if(SetSignal(0,0) & SIG_AUDIO)
  168.         {
  169.             SetSignal(0,SIG_AUDIO);
  170.  
  171.             WaitIO(AudioBlock);
  172.         }
  173.  
  174.         if(CheckIO(AudioBlock) || !SoundPlayed)
  175.         {
  176.             BeginIO(AudioBlock);
  177.  
  178.             SoundPlayed = TRUE;
  179.         }
  180.     }
  181. }
  182.  
  183.     /* OpenSound(UBYTE *Name):
  184.      *
  185.      *    Load an IFF-8SVX sound file instead of the standard
  186.      *    beep (yes, this is a zany feature nobody will ever need!).
  187.      */
  188.  
  189. BYTE
  190. OpenSound(UBYTE *Name)
  191. {
  192.     STATIC ULONG SoundProps[] = { '8SVX', 'VHDR' };
  193.  
  194.     struct IFFHandle    *Handle;
  195.     struct StoredProperty    *Prop;
  196.     struct Voice8Header    *VoiceHeader;
  197.  
  198.     BYTE             Success = FALSE;
  199.  
  200.         /* Allocate an IFF handle. */
  201.  
  202.     if(Handle = AllocIFF())
  203.     {
  204.             /* Open the file. */
  205.  
  206.         if(Handle -> iff_Stream = Open(Name,MODE_OLDFILE))
  207.         {
  208.                 /* Say it's a DOS file. */
  209.  
  210.             InitIFFasDOS(Handle);
  211.  
  212.                 /* Open the file for reading. */
  213.  
  214.             if(!OpenIFF(Handle,IFFF_READ))
  215.             {
  216.                     /* Remember VHDR-chunks encountered. */
  217.  
  218.                 if(!PropChunks(Handle,&SoundProps[0],1))
  219.                 {
  220.                         /* Stop at the body chunk. */
  221.  
  222.                     if(!StopChunk(Handle,'8SVX','BODY'))
  223.                     {
  224.                             /* Start scanning... */
  225.  
  226.                         if(!ParseIFF(Handle,IFFPARSE_SCAN))
  227.                         {
  228.                                 /* Obtain the stored VHDR-chunk. */
  229.  
  230.                             if(Prop = FindProp(Handle,'8SVX','VHDR'))
  231.                             {
  232.                                 VoiceHeader = (struct Voice8Header *)Prop -> sp_Data;
  233.  
  234.                                     /* Compressed data not supported so far. */
  235.  
  236.                                 if(!VoiceHeader -> sCompression)
  237.                                 {
  238.                                     struct ContextNode *ContextNode;
  239.  
  240.                                         /* Inquire current chunk. */
  241.  
  242.                                     if(ContextNode = CurrentChunk(Handle))
  243.                                     {
  244.                                             /* We don't support double-buffering, this
  245.                                              * is only a *simple* example.
  246.                                              */
  247.  
  248.                                         if(ContextNode -> cn_Size < 102400)
  249.                                         {
  250.                                                 /* Allocate the data storage. */
  251.  
  252.                                             if(SoundInfo . Data = AllocVec(ContextNode -> cn_Size,MEMF_CHIP))
  253.                                             {
  254.                                                     /* Play either the one-shot or the continuous
  255.                                                      * part, not both.
  256.                                                      */
  257.  
  258.                                                 SoundInfo . Length    = VoiceHeader -> oneShotHiSamples ? VoiceHeader -> oneShotHiSamples : VoiceHeader -> repeatHiSamples;
  259.  
  260.                                                     /* Calculate recording rate. */
  261.  
  262.                                                 SoundInfo . Rate    = (GfxBase -> DisplayFlags & PAL ? 3546895 : 3579545) / VoiceHeader -> samplesPerSec;
  263.  
  264.                                                     /* Calculate volume. */
  265.  
  266.                                                 SoundInfo . Volume    = (VoiceHeader -> volume * 64) / 0x10000;
  267.  
  268.                                                     /* Read the data. */
  269.  
  270.                                                 if(ReadChunkBytes(Handle,SoundInfo . Data,ContextNode -> cn_Size))
  271.                                                     Success = TRUE;
  272.                                                 else
  273.                                                     FreeVec(SoundInfo . Data);
  274.                                             }
  275.                                         }
  276.                                     }
  277.                                 }
  278.                             }
  279.                         }
  280.                     }
  281.                 }
  282.  
  283.                     /* Make iffparse clean up after us. */
  284.  
  285.                 CloseIFF(Handle);
  286.             }
  287.  
  288.                 /* Close the DOS handle. */
  289.  
  290.             Close(Handle -> iff_Stream);
  291.         }
  292.  
  293.             /* Free the IFF handle. */
  294.  
  295.         FreeIFF(Handle);
  296.     }
  297.  
  298.         /* Remember success/failure. */
  299.  
  300.     HasSound = Success;
  301.  
  302.     return(Success);
  303. }
  304.